package com.example.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.entity.PsLineInfo;
import com.example.entity.PsPoleInfo;
import com.example.service.PsLineInfoService;
import com.example.service.PsPoleInfoService;
import com.example.vo.PsPoleInfoVo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yt
 * @since 2024-04-23
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/psPoleInfo")
@CrossOrigin
public class PsPoleInfoController {
    private final PsPoleInfoService psPoleInfoService;
    private final PsLineInfoService psLineInfoService;

    /**
     * 分页查询
     *
     * @param vo lineCode 路线
     * @param vo isStart  是否开启0未开启1开启
     * @return
     */
    @RequestMapping(value = "/psLineInfoPage", method = RequestMethod.GET)
    public Result psLineInfoList(PsPoleInfoVo vo) {
        if (vo.getLineCode() != null && ! vo.getLineCode().equals("")) {
            String lineCode = vo.getLineCode();
            String substring = lineCode.substring(0, 2);
            vo.setLineCode(substring);
            System.out.println(substring);
        }
        Page<PsPoleInfo> page = new Page<>(vo.getPageNum(), vo.getPageSize());
        IPage<PsPoleInfo> bySearch = psPoleInfoService.findBySearch(page, vo);
        return Result.success(bySearch);
    }

    @RequestMapping(value = "/selectLineCode", method = RequestMethod.GET)
    public Result selectLineCode() {
        List<PsLineInfo> psLineInfos = psLineInfoService.selectLineCode();
        return Result.success(psLineInfos);
    }

    @RequestMapping(value = "/selectIsStart", method = RequestMethod.GET)
    public Result selectIsStart() {
        List<PsLineInfo> psLineInfos = psLineInfoService.selectIsStart();
        return Result.success(psLineInfos);
    }


    //,produces = "text/html;charset=UTF-8"
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(@RequestBody PsPoleInfo psPoleInfo) {
//        根据id来判断是否是新增或者修改
        if (psPoleInfo.getId() == null) {
            psPoleInfoService.add(psPoleInfo);
        } else {
            psPoleInfoService.updatePsPoleInfo(psPoleInfo);
        }
        return Result.success();
    }

    @RequestMapping(value = "/selectAllpoleCodeAsc", method = RequestMethod.GET)
    public Result selectAllpoleCodeAsc() {
        List<PsPoleInfo> roleInfo = psPoleInfoService.selectAllpoleCodeAsc();
        return Result.success(roleInfo);
    }

    @RequestMapping(value = "/selectAllPoleCodeDesc", method = RequestMethod.GET)
    public Result selectAllPoleCodeDesc() {
        List<PsPoleInfo> roleInfo = psPoleInfoService.selectAllPoleCodeDesc();
        return Result.success(roleInfo);
    }


    @RequestMapping(value = "/updateIsStart", method = RequestMethod.POST)
    public Result updateIsStart(@RequestBody Map<String, Object> map) {
        PsPoleInfo psPoleInfo = new PsPoleInfo();
        psPoleInfo.setId((Integer) map.get("id"));
        psPoleInfo.setIsstart((Integer) map.get("isstart"));
        psPoleInfoService.updateIsStart(psPoleInfo);
        return Result.success();
    }

    /**
     * 删除杆塔
     */
    @RequestMapping(value = "/delPsLineInfo", method = RequestMethod.GET)
    public Result delPsLineInfo(Integer id) {
        psPoleInfoService.delete(id);
        return Result.success();
    }
}
